const path = require('path');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const url = require('url');
const query = require('querystring')


module.exports = {
    entry: './src/js/index.js',
    output: {
        filename: 'js/index.js',
        path: path.resolve(__dirname, "./dist")
    },
    module: {
        rules: [
            // js
            {
                test: /\.js$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env"]
                    }
                }
            },
            // scss
            {
                test: /\.scss$/,
                use: ExtractTextWebpackPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'sass-loader']
                })
            },
            // html
            {
                test: /\.html$/,
                use: {
                    loader: 'html-loader',
                    options: {
                        filename: '[name].[ext]'
                    }
                }
            }
        ]
    },
    plugins: [
        new ExtractTextWebpackPlugin({
            filename: "style/[name].css"
        }),
        new HtmlWebpackPlugin({
            template: './src/index.html'
        })
    ],
    devServer: {
        port: 3500,
        host: 'localhost',
        open: true,
        before(app) {
            app.post('/api/user', (req, res, next) => {
                let data = '';
                req.on('data', function(chunk) {
                    data += chunk
                })
                req.on('end', function() {
                    let newdata = query.parse(data)
                        // console.log(data)
                        // console.log(newdata.user)
                        // console.log(newdata.pwd)
                    if (newdata.user == "张三" && newdata.pwd == "123456") {
                        res.json({
                            code: 1,
                            data: '登陆成功'
                        })
                    } else {
                        res.json({
                            code: 0,
                            data: '登陆失败'
                        })
                    }
                })
            })
        }
    }
}